package com.ybkj.daijia.server.event.listener;

import com.ybkj.daijia.PushData;
import com.ybkj.daijia.Setting;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.api.gexin.GeTuiMember;
import com.ybkj.daijia.server.event.model.MemberBookTimeZhuanEvent;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.MemberService;
import com.ybkj.daijia.server.service.SmsTempletService;
import java.io.IOException;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class MemberBookTimeZhuanEventListener implements
    ApplicationListener<MemberBookTimeZhuanEvent> {

    private static final Logger logger = LoggerFactory
        .getLogger(MemberBookTimeZhuanEventListener.class);

    @Autowired
    private SmsTempletService smsTempletService;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private MemberService memberService;

    @Override
    @Async
    public void onApplicationEvent(MemberBookTimeZhuanEvent event) {

        logger.debug("admin MemberNoticeEvent event:{}", "");

        List<Passenger> passengers = event.getPassengers();

        Setting setting = settingUtils.get();

        for (Passenger passenger : passengers) {

            passenger.setCid(memberService.findCid(passenger.getId()));

            if (StringUtils.isNotBlank(passenger.getCid())) {
                try {
                    //发送设备推送
                    PushData pushData = new PushData();
                    pushData.setType("NEW_BOOK_TIME");
                    pushData.setTitle("新的预约时间");
                    if (null != pushData.getLink()) {
                        GeTuiMember.notice(passenger, pushData, setting);
                    }
                    GeTuiMember.push(passenger, pushData, setting);

                } catch (IOException e) {
                    logger.debug("push error : " + e.getMessage());
                }
            }
        }


    }

}
